<import file="block/common.html" name="url"/>
<div class="layui-fluid">
	<div class="layui-form">
		<div class="layui-form-item">
			<label class="layui-form-label required">选择栏目</label>
			<div class="layui-input-inline">
				<select name="cid" lay-verify="">
					<php foreach="$list as $key=>$vo">
					  <option value="<{$vo['id']}>" <?php echo $info['cid'] == $vo['id']?'selected=""':''; echo $vo['type']==0 ?'disabled=""':'';?>><{$vo['cname']}></option>
					</php> 
					</select> 
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label required">标题</label>
			<div class="layui-input-block">
				<input type="text" name="title" lay-verify="required" lay-reqtext="标题不能为空" placeholder="请输入标题"
					value="<?php echo $info['title'];?>" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item layui-form-text">
			<label class="layui-form-label">内容</label>
			<div class="layui-input-block">
				<textarea id="contentEdit" name="content"><{$info['content']}></textarea>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label required">审核人</label>
			<div class="layui-input-inline">
				<input type="text" name="examine" lay-verify="required" lay-reqtext="审核人不能为空" placeholder="请输入审核人"
					value="<?php echo $info['examine'];?>" class="layui-input">
			</div>
		</div>
		<?php if(sysrolTF("content_attribute")){ ?>
		<div class="layui-form-item">
			<label class="layui-form-label required">内容属性</label>
			<div class="layui-input-block">
				<input type="checkbox" name="attribute_a" value="1" title="头条"
					<?php echo $info['attribute_a'] == 1?'checked=""':'';?>>
				<input type="checkbox" name="attribute_b" value="1" title="小头条"
					<?php echo $info['attribute_b'] == 1?'checked=""':'';?>>
				<span id="lunhuantu"><input type="checkbox" name="attribute_c" value="1" title="图片轮换"
					<?php echo $info['attribute_c'] == 1?'checked=""':'';?>></span>
			</div>
		</div>
		<?php } ?>
		<?php if(sysrolTF("upload_imgUpload")){ ?>
		<div class="layui-form-item">
			<label class="layui-form-label required">轮换缩略图</label>
			<div class="layui-input-block">
				<input type="text" id="imageval" name="img" value="<{$info['img']}>" class="layui-input">
				<!-- <button type="button" class="layui-btn" id="thumimg">
				  <i class="layui-icon">&#xe67c;</i>上传图片
				</button> -->
				<button type="button" class="layui-btn" id="getimage" onclick="getFirstImg()">
					<i class="layui-icon">&#xe60d;</i>获取内容第一张
				</button>
			</div>
		</div>
		<?php } ?>
		<?php if(sysrolTF("upload_attmentUpload")){ ?>
		<div class="layui-form-item">
			<label class="layui-form-label required">附件</label>
			<div class="layui-input-block">
				<div id="attval" class="cxuu_attmentupload">
				<?php
					foreach($infoatt as $att){
						echo '<span id="attidd'.$att['attid'].'" class="layui-badge layui-bg-blue">ID：'.$att['attid'].' 文件名：'.$att['priname'].'<a href="javascript:;" onclick="attColse('.$att['attid'].')" ><i class="layui-icon layui-icon-close"></i></a></span>';
					}					
				?>
				</div>
				<input type="hidden" name="attid" value="<{$info['attid']}>" id="attid">
				<button type="button" class="layui-btn" id="attupload">
					<i class="layui-icon">&#xe67c;</i>上传附件
				</button>
			</div>
		</div>
		<?php } ?>
		<div class="layui-form-item">
			<label class="layui-form-label">状态</label>
			<div class="layui-input-inline">
				<?php if(sysrolTF("content_status")){ ?><input type="radio" name="status" value="1" title="发布"
					<?php if($info['status'] == 1){echo 'checked';}?>><?php } ?>
				<input type="radio" name="status" value="0" title="草稿"
					<?php if($info['status'] == 0){echo 'checked';}?>>
			</div>
		</div>
		<div class="layui-form-item">
			<div class="layui-input-block">
				<input type="hidden" name="id" value="<{$info['id']}>">
				<button class="layui-btn" lay-submit lay-filter="saveBtn"><?php echo lang('save');?></button>
			</div>
		</div>
	</div>
</div>
<script type="text/javascript" src="<?php echo U_RES;?>/lib/tinymce/tinymce.min.js?v=5.02"></script>
<script>
	layui.use(['form', 'table', 'upload'], function () {
		var form = layui.form,
			layer = layui.layer,
			table = layui.table,
			upload = layui.upload,
			$ = layui.$;

		tinymce.init({
			selector: '#contentEdit',
			language: 'zh_CN',
			relative_urls: false,
			remove_script_host: true,
			plugins: 'print preview searchreplace autolink directionality visualblocks visualchars image link media template code codesample table charmap hr pagebreak nonbreaking anchor insertdatetime advlist lists wordcount imagetools textpattern help emoticons indent2em autoresize lineheight axupimgs',
			toolbar: 'code | undo redo restoredraft | cut copy paste | forecolor bold italic underline link | alignleft aligncenter alignright alignjustify outdent indent | fontselect fontsizeselect | table <?php if(sysrolTF("upload_tinymceUpload")){ ?>image<?php } ?> charmap emoticons insertdatetime preview | indent2em lineheight <?php if(sysrolTF("upload_tinymceUpload")){ ?>axupimgs<?php } ?>',
			autosave_interval: "20s",//自动存稿时间
			font_formats: '微软雅黑=Microsoft YaHei,sans-serif;仿宋_GB2312=仿宋_GB2312,Microsoft YaHei,sans-serif;宋体=宋体;仿宋=仿宋;黑体=黑体',
			height: 650, //编辑器高度
			min_height: 400,
			draggable_modal: true,
			images_upload_base_path: '/',
			images_reuse_filename: true,
			images_upload_handler: function (blobInfo, succFun, failFun) {
				var xhr, formData;
				var file = blobInfo.blob();//转化为易于理解的file对象
				xhr = new XMLHttpRequest();
				xhr.withCredentials = false;
				xhr.open('POST', '<{caUrl("upload","tinymceUpload")}>');
				xhr.onload = function () {
					var json;
					if (xhr.status != 200) {
						failFun('HTTP Error: ' + xhr.status);
						return;
					}
					json = JSON.parse(xhr.responseText);
					if (!json || typeof json.location != 'string') {
						failFun('Invalid JSON: ' + xhr.responseText);
						return;
					}
					succFun(json.location);
				};
				formData = new FormData();
				formData.append('file', file, file.name + '.jpg');
				xhr.send(formData);
			}
		});

		form.render();

		//监听提交 +=>
		form.on('submit(saveBtn)', function (data) {
			//console.log(tinyMCE.editors[0].getContent());
			$.post('<{caUrl("content","edit")}>',
				{
					cid: data.field.cid,
					title: data.field.title,
					attid: data.field.attid,
					attribute_a: data.field.attribute_a,
					attribute_b: data.field.attribute_b,
					attribute_c: data.field.attribute_c,
					examine: data.field.examine,
					img: data.field.img,
					content: tinyMCE.editors[0].getContent(),
					status: data.field.status,
					id: data.field.id
				},
				function (res) {
					if (res.status == 1) {
						layer.msg(res.info, { icon: 1, time: 1000 }, function () {
							loadMain('<{caUrl("content","index")}>&cid=' + res.cid);
						});
					} else {
						layer.msg(res.info, { icon: 2, anim: 6, time: 5000 });
					}
				});
			return false;
		});


		var uploadInst = upload.render({
			elem: '#thumimg' //绑定元素
			, url: '<{caUrl("upload","imgUpload")}>' //图片上传接口
			, size: '<{$imguploadsize}>'
			, accept: 'images'
			, acceptMime: 'image/*'
			//,acceptMime: 'image/jpg, image/png'
			, exts: '<{$imguploadext}>'
			, done: function (res) {
				if (res.status == 1) {
					$('#imageval').val(res.result[0].src);
					layer.msg(res.info);
				} else {
					layer.msg(res.info);
				}
			}
			, error: function () {
				//请求异常回调
			}
		});
		var uploadAttment = upload.render({
			elem: '#attupload' //绑定元素
			, url: '<{caUrl("upload","attmentUpload")}>' //附件上传接口
			, size: '<{$attuploadsize}>'
			, accept: 'file'
			, acceptMime: 'file/*'
			//,acceptMime: 'image/jpg, image/png'
			//, exts: 'zip|rar|7z|doc|docx|xlsx|xls|'
			, exts: '<{$attuploadext}>'
			, done: function (res) {
				if (res.status == 1) {
					$('#attval').append('<span id="attidd'+ res.prekey +'" class="layui-badge layui-bg-blue"> ID：'+res.prekey+' 文件名：'+ res.result[0].originName + ' <a href="javascript:;" onclick="attColse('+res.prekey+')" ><i class="layui-icon layui-icon-close"></i></a></span>');
					var input = $("#attid").val();
					if(input != "" && input != undefined){
						$("#attid").val($("#attid").val()+","+res.prekey);
					}else{
						$('#attid').val(res.prekey);
					}
					//res.result[0].src + res.result[0].suffix + res.result[0].size
					layer.msg(res.info);
				} else {
					layer.msg(res.info);
				}
			}
			, error: function () {
				//请求异常回调
			}
		});

	});
	//删除已经上传附件
	function attColse(id){
		layer.confirm('真的删除这个附件吗？',{icon: 3, title:'警告'},  function (index) {
			$.get('<{caUrl("attment","del")}>', { attid: id},function(data){
				if(data.status==1){
					layer.msg("附件: " + data.info);
					var input = $("#attid").val();
					var newarr = input.split(",");
					newarr.splice($.inArray(""+id+"",newarr),1);
					var newstring = newarr.join(",");
					$('#attid').val(newstring);
					$('#attidd'+id).remove();
				}else{
					layer.msg("附件: " + data.info+"请查看是否有删除附件权限");
				}
			});
		layer.close(index);
		});
	}

	//获取编辑器内容的第一张图片
	function getFirstImg() {
		var content = tinyMCE.editors[0].getContent();
		var getval = $(content).find("img").eq(0).attr("src");
		if (getval.length > 0) {
			$('#imageval').val(getval);
			$('#lunhuantu').show(); //如果有内容，显示轮换选择框
		}
	}
	//如果没有提取缩略图，就不能选择 轮换图
	var imageval = $('#imageval').val();
	if (imageval.length < 1) {
		$('#lunhuantu').hide();
	} else {
		$('#lunhuantu').show();
	}
	$('#imageval').bind('input propertychange', function () {
		if ($(this).val().length) {
			$('#lunhuantu').show();
		} else {
			$('#lunhuantu').hide();
		}

	});
</script>